﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json.Linq;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.MerchantsSettings
{
    /// <summary>
    /// 商户积分管理
    /// </summary>
    public class MerchantsIntegralManageController : BaseController
    {
        // 操作员
        private static Model.View_OperUser operUser;

        // 更新
        public string ExecuteUpdate(Model.View_Order_Int model, int key)
        {
            try
            {
                if (model != null)
                {
                    // string result = SRS.SetOrder_Int (operUser.OperID, ObjectToJson(model), key, YZM, 1);
                    // TAG = result.IndexOf("\"BS\":\"1\"");
                }
            }
            catch (Exception e)
            {
                string msg = e.Message;
            }
            return TAG + "";
        }

        public ActionResult getMer()
        {
            try
            {
                List<Model.View_Order_MerSet> list = new List<Model.View_Order_MerSet>();//定义商户
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                string result = SRS.GetMer(user.OperID, "", 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        foreach (JToken item in jieguo)
                        {
                            Model.View_Order_MerSet m = new Model.View_Order_MerSet();
                            m.MerID = item["MerID"].ToString();
                            m.MerName = item["MerName"].ToString();
                            list.Add(m);
                        }
                    }
                    return Json(list, JsonRequestBehavior.AllowGet);
                }
                return Json(searcho["Msg"].ToString());
            }
            catch (Exception ex)
            {                
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 获取所有商户
        /// </summary>
        /// <param name="jqgridparam"></param>
        /// <returns></returns>
        public ActionResult getMerInt(JqGridParam jqgridparam)
        {
            try
            {
                Page<Model.View_Order_Int> page = new Page<Model.View_Order_Int>();
                List<Model.View_Order_Int> list = new List<Model.View_Order_Int>();//定义商品分类设置
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                string result = SRS.GetOrder_MerInt(user.OperID, "", 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    if (jieguo.Count > 0)
                    {
                        foreach (JToken item in jieguo)
                        {
                            Model.View_Order_Int m = new Model.View_Order_Int();
                            m.IntID = item["IntID"].ToString();
                            m.MerID = item["MerID"].ToString();
                            m.MerName = item["MerName"].ToString();
                            m.IntUn = int.Parse(item["IntUn"].ToString());
                            m.IntY = int.Parse(item["IntY"].ToString());
                            m.IntChe = int.Parse(item["IntChe"].ToString());
                            m.IsOpen = int.Parse(item["IsOpen"].ToString());
                            list.Add(m);
                        }
                    }
                    jqgridparam.records = list.Count;//查询的数据总数
                    var data = new
                    {
                        total = list.Count.ToString(),
                        rows = page.FindListPage(list, ref jqgridparam)//返回当前页的数据
                    };
                    return Json(data);
                }
                return Json(searcho["Msg"].ToString());
            }
            catch (Exception ex)
            {               
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 获取系统的增删查改的权限
        /// </summary>
        /// <returns></returns>
        public ActionResult getPower()
        {
            try
            {
                string[] PowerS = null;
                //string RetPower = "";
                Models.ControlPower RetPower = new Models.ControlPower();
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];
                if (user != null)
                {
                    PowerS = user.RolePower.Split(',');//权限码
                    //拼接增删查改的权限
                    foreach (string pow in PowerS)
                    {
                        if (pow == "009002002")
                        {
                            RetPower.Add = 1;
                        }
                        else if (pow == "009002003")
                        {
                            RetPower.Dell = 1;
                        }
                        else if (pow == "009002004")
                        { RetPower.Edit = 1; }
                    }
                }
                else
                {                
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面                  
                }
                return Json(RetPower);
            }
            catch (Exception ex)
            {              
                return Json("操作异常：" + ex + "请联系管理员解决");
            }
        }

        /// <summary>
        /// 保存商户积分
        /// </summary>
        /// <param name="model">积分管理参数</param>
        /// <returns></returns>
        public ActionResult Submit(int Tag, Model.Order_Int_T model)
        {
            if (model != null)
            {
                // 获取操作员
                try  //czx
                {
                    Model.View_OperUser operUser = Session[SessionStorage.OperUser] as Model.View_OperUser;
                    string value = ObjectToJson(model);
                    string result = "";
                    string tojson = ObjectToJson(model).ToString();
                    result = SRS.SetOrder_Int(operUser.OperID, tojson, Tag, YZM, 1);
                    JObject jb = JObject.Parse(result);
                    if (jb["BS"].ToString() == "1")
                        return Content(jb["BS"].ToString());
                    else
                        return Content(jb["Msg"].ToString());
                }
                catch (Exception ex)
                {
                    return RedirectToAction("LoginPage", "Login"); //czx:当session失效时，自动跳转到登录页面
                }
            }
            return Content("数据异常，请联系管理员");
        }

        // 对话框
        public ActionResult Dialog(Model.View_Order_Int model)
        {
            #region 构建数据
            if (model == null)  // 添加
            {
                // 标识为添加
                ViewBag.ACTION = "BtnAdd";
                // 原始数据
                ViewData.Model = null;
                // 积分转换单位
                ViewBag.IntUnList = GetIntUnList(null);
                // 转换金额处理
                ViewBag.IntYList = GetIntYList(null);
                // 是否打开
                ViewBag.IsOpenList = GetIsOpenList(null);
            }
            else
            {
                // 标识为编辑
                ViewBag.ACTION = "BtnEdit";
                // 原始数据
                ViewData.Model = model;
                // 积分转换单位
                ViewBag.IntUnList = GetIntUnList(model);
                // 转换金额处理
                ViewBag.IntYList = GetIntYList(model);
                // 是否打开
                ViewBag.IsOpenList = GetIsOpenList(model);
            }
            #endregion

            return View();
        }
        // 主页
        public ActionResult Index()
        {
            operUser = Session[SessionStorage.OperUser] as Model.View_OperUser;

            if (operUser != null)
            {
                ViewBag.UserPowers = operUser.RolePower;
            }
            else
            {
                return RedirectToAction("LoginPage", "Login"); //czx:当session失效时，自动跳转到登录页面

            }

            return View();

        }

    }
}
